package com.phamloc.shootingchicken.manager;

import java.io.IOException;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.debug.Debug;

import android.graphics.Color;

import com.phamloc.shootingchicken.GameActivity;
import com.phamloc.shootingchicken.extra.AudioPlayer;


public class ResourcesManager {
	private static final ResourcesManager INSTANCE = new ResourcesManager();
	public  GameActivity activity;
	public  BoundCamera camera;
	public  VertexBufferObjectManager vbom;
	public  Engine engine;
	//font
	public Font font;
	//splash scene resources
	public BitmapTextureAtlas splashTextureAtlas;
	public ITextureRegion  splashRegion;
	//menu scene resource
	public BuildableBitmapTextureAtlas menuTextureAtlas;
	public ITiledTextureRegion menuRegion;
	public ITiledTextureRegion menuButtonRegion;
	public ITextureRegion menuBackgroud;
	public AudioPlayer audioPlayer;
	 public static void prepareManager(Engine engine, GameActivity activity, BoundCamera camera, VertexBufferObjectManager vbom)
	    {
	        getInstance().engine = engine;
	        getInstance().activity = activity;
	        getInstance().camera = camera;
	        getInstance().vbom = vbom;
	    }
	    
	    public static ResourcesManager getInstance()
	    {
	        return INSTANCE;
	    }

		public void loadSplashScreen() {
			BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/splash/");
			splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
			splashRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.jpg", 0, 0);
			splashTextureAtlas.load();
		}

		public void unloadSplashScreen() {
			splashTextureAtlas.unload();
			splashRegion = null;
		}

		public void loadMenuResources() {
			loadMenuGraphics();
	        loadMenuAudio();
	        loadGameFonts();
		}

		private void loadGameFonts() {
			FontFactory.setAssetBasePath("font/");
	        final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
	        font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50, true, Color.WHITE, 2, Color.BLACK);
	        font.load();
		}

		private void loadMenuAudio() {
			audioPlayer = new AudioPlayer(true, true);
			try {
				audioPlayer.loadMusicAndSound(engine.getSoundManager(), engine.getMusicManager(), activity);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		private void loadMenuGraphics() {
			BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
			menuTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024,TextureOptions.BILINEAR);
			menuRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(menuTextureAtlas, activity, "menu.png", 1, 3);
			menuButtonRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(menuTextureAtlas, activity, "button_menu.png", 5, 1);
			menuBackgroud = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "menubackgroud.jpg");
			try 
	    	{
	    	    this.menuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
	    	    this.menuTextureAtlas.load();
	    	} 
	    	catch (final TextureAtlasBuilderException e)
	    	{
	    	        Debug.e(e);
	    	}
		}

		public void unloadMenuTextures() {
			menuTextureAtlas.unload();
		}
		
		public void loadGameResources() {
			// TODO Auto-generated method stub
			
		}
		
		public void unloadGameTextures() {
			// TODO Auto-generated method stub
			
		}

		public void loadMenuTextures() {
			menuTextureAtlas.load();
		}
}
